package com.vcourse.service.edu.controller.admin;

import cn.hutool.core.collection.CollUtil;
import com.vcourse.commons.result.R;
import com.vcourse.commons.utils.PageUtils;
import com.vcourse.service.edu.service.CourseService;
import com.vcourse.service.edu.service.VideoService;
import com.vcourse.service.edu.vo.CourseInfoForm;
import com.vcourse.service.edu.vo.CoursePublishVo;
import com.vcourse.service.edu.vo.CourseQueryVo;
import com.vcourse.service.edu.vo.TeacherQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Zengsh123
 * @since 2020-05-25
 */
@Api("课程管理")
@RestController
@RequestMapping("admin/edu/course")
@Slf4j
public class CourseController {
    @Resource
    private CourseService courseService;
    @Resource
    private VideoService videoService;

    @ApiOperation("保存课程")
    @PostMapping("/saveCourseInfo")
    public R saveCourseInfo(@ApiParam("课程信息") @RequestBody CourseInfoForm courseInfoForm) {
        String courseId = courseService.saveCourseInfo(courseInfoForm);
        return R.ok().data(courseId);
    }

    @ApiOperation("根据课程ID查询课程信息")
    @GetMapping("/getCourseById/{id}")
    public R getCourseById(@ApiParam("课程Id") @PathVariable String id) {
        CourseInfoForm courseInfoForm = courseService.getCourseById(id);
        if (courseInfoForm != null) {
            return R.ok().data(courseInfoForm);
        }
        return R.ok().message("课程信息不存在");
    }

    @ApiOperation("更新课程信息")
    @PutMapping("/updateCourse")
    public R updateCourse(@ApiParam("课程信息") @RequestBody CourseInfoForm courseInfoForm) {
        courseService.updateCourse(courseInfoForm);
        return R.ok().message("课程信息更新成功");
    }

    @ApiOperation("分页查询课程")
    @GetMapping("/list/{currentPage}/{limit}")
    public R listPage(@ApiParam("当前页码") @PathVariable Long currentPage,
                      @ApiParam("每页条数") @PathVariable Long limit,
                      @ApiParam("查询对象")  CourseQueryVo queryVo) {
        PageUtils listPage = courseService.queryPage(currentPage, limit, queryVo);
        return R.ok().data(listPage);
    }

    @ApiOperation("删除单个课程")
    @DeleteMapping("/delete/{id}")
    public R delete(@ApiParam("课程ID") @PathVariable String id) {
        //删除视频：VOD
        videoService.removeMediaVideoByCourseId(CollUtil.newArrayList(id));

        boolean res = courseService.removeCourse(Collections.singletonList(id));
        if (res) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation("批量删除课程")
    @DeleteMapping("/batchDelete")
    public R batchDelete(@ApiParam("课程Id列表") @RequestBody List<String> idList) {
        //删除视频：VOD
        videoService.removeMediaVideoByCourseId(idList);

        boolean res = courseService.removeCourse(idList);
        if (res) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation("根据ID获取课程发布信息")
    @GetMapping("coursePublish/{id}")
    public R getCoursePublishVoById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String id){

        CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);
        if (coursePublishVo != null) {
            return R.ok().data(coursePublishVo);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据id发布课程")
    @PutMapping("publishCourse/{id}")
    public R publishCourseById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String id){

        boolean result = courseService.publishCourseById(id);
        if (result) {
            return R.ok().message("发布成功");
        } else {
            return R.error().message("数据不存在");
        }
    }
}

